/*
 页面名称：管理员 -添加员工
 作者: 苗怀雨
 时间:2017年12月31日
 最后编辑：2017年12月31日20:20:13
 */

$(document).ready(function() {
    var buttons = $("button.btn");
});


function searchPayments(e,username,type) {
    var $this = $(e);
    var pageNum = $this.text();
    var pageSize = $("#pageSize").val();
    var $username = username;
    var $type = type;

    var $form = $("<div class='postForm'></div>");
    var $elem = $("<div class='postParam'></div>");

    $elem.attr("id","pageNum");
    $elem.text(pageNum);
    $form.append($elem.clone());

    $elem.attr("id","pageSize");
    $elem.text(pageSize);
    $form.append($elem.clone());

    $elem.attr("id","username");
    $elem.text($username);
    $form.append($elem.clone());

    $elem.attr("id","type");
    $elem.text($type);
    $form.append($elem.clone());

    postForm("searchPayments",$form);
}

function ensureBorrow(e) {
    var $modal = $(".bs-example-modal-eb");
    var $tableElemenets = $(e).parents("tr").children('td');
    var bookName = $tableElemenets.eq(5).text();
    var borrowId = $tableElemenets.eq(1).text();
    var staffName = $tableElemenets.eq(3).text();
    var type = $tableElemenets.eq(13).children("span").text();

    $("h4",$modal).eq(1).text("确认操作:员工"+ staffName + "  " + type.substr(0,2) + bookName);       //设置标题名称
    $(".btn-primary",$modal).attr("onclick","ensure("+borrowId+");");  //设置确定按钮
}

//确认操作
function ensure(borrowId) {
    var $modal = $(".bs-example-modal-eb");
    var $closeButton = $("button",$modal).eq(1);        //弹出框关闭按钮
    var $ensureButton = $("button",$modal).eq(2);       //弹出框确定按钮

    var $form = $("<div class='postForm'></div>");
    var $elem = $("<div class='postParam'></div>")

    $elem.attr("id","borrowId");
    $elem.text(borrowId);
    $form.append($elem.clone());

    $elem.attr("id","type");
    $elem.text(1);
    $form.append($elem.clone());

    $ensureButton.attr("disabled","disabled"); //设置编辑确定按钮暂时不可用
    postForm("ensureBorrow",$form);
}

function dealInfo(e,type,admin) {
    var $modal = $(".bs-example-modal-lb");
    var $titles = $("h4",$modal);
    var $tableElemenets = $(e).parents("tr").children('td');
    var bookName = $tableElemenets.eq(5).text();
    var borrowId = $tableElemenets.eq(1).text();
    var staffId = $tableElemenets.eq(2).text();
    var staffName = $tableElemenets.eq(3).text();
    var startTime = $tableElemenets.eq(10).text();
    var endTime = $tableElemenets.eq(11).text();
    var borrowDays = $tableElemenets.eq(12).text();
    var allDays = $tableElemenets.eq(13).text();
    var bookPrice = $tableElemenets.eq(16).text();
    var date = new Date();

    if(type == "5"){      //挂失
        $titles.eq(0).text("挂失处理");
        $titles.eq(1).text("挂失申请");       //设置标题名称
        $titles.eq(1).children("span").text(staffName);
        $titles.eq(1).prepend("<span class='green'>"+staffName +"</span>");
        $titles.eq(2).children("span").text("《"+bookName + "》");

        if(borrowDays >= allDays){
            $titles.eq(3).children("span").text(1.0 * bookPrice + 10.0 + (1.0*borrowDays - 1.0*allDays) * 0.3);
        }else{
            $titles.eq(3).children("span").text(1.0 * bookPrice + 10.0);
        }

        $titles.eq(4).children("span").text(admin);
        $titles.eq(5).children("span").text(date.toLocaleDateString());
        $titles.eq(7).children("span").text(borrowId);
        $titles.eq(8).children("span").text(staffId);
    }else{      //超期
        $titles.eq(0).text("超期处理");
        $titles.eq(1).text("超期处理申请");       //设置标题名称
        $titles.eq(1).children("span").text(staffName);
        $titles.eq(1).prepend("<span class='green'>"+staffName +"</span>");

        $titles.eq(2).children("span").text("《"+bookName + "》");
        $titles.eq(3).children("span").text(0.3* (1.0*borrowDays - 1.0*allDays));
        $titles.eq(4).children("span").text(admin);
        $titles.eq(5).children("span").text(date.toLocaleDateString());

        $titles.eq(6).children("span").text(1.0 * borrowDays - 1.0* allDays);
        $titles.eq(6).css("display","block");

        $titles.eq(7).children("span").text(borrowId);
        $titles.eq(8).children("span").text(staffId);
        $titles.eq(9).children("span").text("1");
    }

    $(".btn-primary",$modal).attr("onclick","deal("+borrowId+");");  //设置确定按钮
}

//确认操作
function deal(borrowId) {
    var $modal = $(".bs-example-modal-lb");
    var $closeButton = $("button",$modal).eq(1);        //弹出框关闭按钮
    var $ensureButton = $("button",$modal).eq(2);       //弹出框确定按钮

    $ensureButton.attr("disabled","disabled"); //设置编辑确定按钮暂时不可用
    postForm("deal",$modal.find(".postForm"));
}

function checkEvaluate(e) {
    var $this = $(e);
    var $tableElemenets = $this.parents("tr").children('td');
    var bookId = $tableElemenets.eq(17).text();
    var staffId = $tableElemenets.eq(2).text();

    var $form = $("<div class='postForm'></div>");
    var $elem = $("<div class='postParam'></div>");

    $elem.attr("id","bookId");
    $elem.text(bookId);
    $form.append($elem.clone());

    $elem.attr("id","staffId");
    $elem.text(staffId);
    $form.append($elem.clone());

    postForm("checkEvaluatePage",$form);
}